#Written by MMCOWBOY
import sys
import enemy
import item

class map_proccessor:
    def __init__(self, screen_size, players, filename = "1.map", path = "data/map/"):

        self.players = players                      # player list
        self.cur_level = filename.split('.')[0]     # stage name
        self.screen_size = screen_size              # screen size
        self.obj_info_list = []                     # object infomation list
        
        self.eof = False                         # if there is no object any more

        for line in open(path + filename):
            line_item = line.split(' ')
            self.obj_info_list.append(line_item[0:-1])
        self.obj_info_list.sort(lambda x, y: cmp(x[0],y[0]))
        temp_line = self.obj_info_list.pop(0)
        self.boss_num = temp_line[1]
        
    def is_eof(self):
        return self.eof

    def get_cur_level(self):
        return self.cur_level

    def get_boss_num(self):
        return int(self.boss_num)

    def get_top_info(self, current_time):
        if (len(self.obj_info_list) == 0):
            self.eof = True
            return -1       #if the list is empty return EOF
        else:
            if (current_time >= int(self.obj_info_list[0][0])):
                return self.obj_info_list.pop(0)    #if the first item of the list is OK, return it
            else:
                return []                           #if there is no item fullfill the request, return an empty list

    def get_available_obj(self, current_time):
        temp_top_info = self.get_top_info(current_time)
        if (temp_top_info == -1):
            return False                               #if the list is empty, return False
        
        if (temp_top_info != []):
            location = int(temp_top_info[1])
            main_type = int(temp_top_info[2])
            sub_type = int(temp_top_info[3])
            motion_type = int(temp_top_info[4])
            item_type = int(temp_top_info[5])

            if (main_type == 1):
                # build enemy (kill players) and add it to enemy's containers
                enemy.enemy_obj(self.screen_size, (location, 0), sub_type, motion_type, item_type, self.players)
                return True
                pass
            elif (main_type == 2):
                # build items (players friendly) and add it to enemy's containers
                item.item_craft_obj(self.screen_size, (location, 0), sub_type, item_type)
                return True
                pass
            elif (main_type == 3):
                # build blocks (kill players or disable players's fire) and add it to enemy's containers
                return True
                pass
            else:
                # nonsense operation
                pass
                return False
                
            return 
        else:
            #if there is no item fullfill the request, return False
            return False                             
